package fr.cdesire.sudoku.applet.action;

import java.util.HashMap;
import java.util.Map;

import fr.cdesire.sudoku.applet.exception.TechnicalException;

public enum SudokuEventEnum {

	CHARGE_GRILLE1("chargeGrille1", ChargerGrille3x3Exemple1Action.class),
	CHARGE_GRILLE2("chargeGrille2", ChargerGrille3x3Exemple2Action.class),
	NOUVELLE_GRILLE("nouvelleGrille", ReinitialiserGrilleAction.class),
	COMPLETE_GRILLE("completeGrille", CompleterGrilleAction.class),
	RESOUDRE_GRILLE("resoudreGrille", ChercherSolutionAction.class);

	private static Map<String, SudokuEventEnum> events;

	private String event;
	private Class<? extends AbstractSudokuAction> action;

	static {
		events = new HashMap<String, SudokuEventEnum>();
		for (SudokuEventEnum sudokuEvent : SudokuEventEnum.values()) {
			events.put(sudokuEvent.event, sudokuEvent);
		}
	}

	SudokuEventEnum(String event, Class<? extends AbstractSudokuAction> action) {
		this.event = event;
		this.action = action;
	}

	public static SudokuEventEnum getSudokuEvent(String event) {
		return events.get(event);
	}

	public AbstractSudokuAction getAction() throws TechnicalException {
		try {
			return action.newInstance();
		} catch (InstantiationException ie) {
			throw new TechnicalException(ie);
		} catch (IllegalAccessException iae) {
			throw new TechnicalException(iae);
		}
	}
}
